﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace WellDone
{
    public sealed partial class CreditPaymentDialog : Page
    {
        public event EventHandler CloseRequested;
        public bool bCanceled = false;
        public decimal dGratuity = 0;
        private decimal dSubtotal = 0;
        private decimal dTax = 0;
        public string strAuthCode = "";
        public CreditPaymentDialog()
        {
            this.InitializeComponent();
        }
        public void init(decimal dSubtotalIn, decimal dTaxIn)
        {
            dSubtotal = dSubtotalIn;
            dTax = dTaxIn;
            // Display subtotal and tax information
            tblkSubtotal.Text = dSubtotal.ToString("N");
            tblkTax.Text = dTax.ToString("N");
            // Display remaining values
            calculateValues();
        }
        private void calculateValues()
        {
            decimal dTip, dTotal;
            // Get the gratuity...
            try
            {
                dTip = Convert.ToDecimal(tbTip.Text);
            }
            catch (FormatException)
            {
                dTip = 0;
            }
            // Set the gratuity amount
            dGratuity = dTip;
            // Determine the total, based on subtotal, tax, and tip
            dTotal = dSubtotal + dTax + dTip;
            // Display results
            tblkTotal.Text = dTotal.ToString("N");
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            // Get the tip
            try
            {
                dGratuity = Convert.ToDecimal(tbTip.Text);
            }
            catch (FormatException)
            {
                dGratuity = 0;
            }
            // Get the auth code
            strAuthCode = tbAuthCode.Text;
            // Close the dialog
            if (this.CloseRequested != null)
            {
                this.CloseRequested(this, EventArgs.Empty);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            // Set the "Canceled" flag
            bCanceled = true;
            // Close the dialog
            if (this.CloseRequested != null)
            {
                this.CloseRequested(this, EventArgs.Empty);
            }
        }

        private void tbTip_TextChanged(object sender, TextChangedEventArgs e)
        {
            calculateValues();
        }
    }
}
